﻿#region Header

/*
Behavioral Rating of Dancing Human Crowds based on Motion Patterns
By

Pascal Hauser 
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

and

Raphael Gfeller
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

*/

#endregion

#region Usings

using System;
using paravili.Services;
using Sebarf.Services.Interfaces;

#endregion

namespace paravili.Steps {
	/// <summary>
	/// reads the frame number, written by the <see cref="FrameNumberVisualizer"/> step from the <see cref="IStatisticValuesLocatorService"/>
	/// based on this value and the given FramesPerSeconds value the current time is calculated and stored by the <see cref="IStatisticValuesLocatorService"/>
	/// </summary>
	public class FrameNumberToTimeConverter : ProcessStepWithMeasurement<Object> {
		#region Public Properties

		[ConfigurabelBooleanValue(Name = "is enable")]
		public bool IsEnable { get; set; }

		[ServiceRequest]
		public IStatisticValuesLocatorService StatisticValuesLocatorService { get; set; }

		[ConfigurabelStringValue(Name = "name of the statisticvalue that contains the framenumber value")]
		public string InputFrameNumberStatisticValue { get; set; }

		[ConfigurabelStringValue(Name = "name of the statisticvalue that the elapsed time value goes to")]
		public string OutputStatisticValue { get; set; }

		[ConfigurableNumericValue(Name = "frames per seconds", RangeFrom = 1, RangeTo = 60)]
		public int FramesPerSeconds { get; set; }

		#endregion

		#region Public Methods

		public FrameNumberToTimeConverter() {
			FramesPerSeconds = 10;
			InputFrameNumberStatisticValue = "FrameNumber";
			OutputStatisticValue = "ElapsedTime";
			IsEnable = true;
		}

		#endregion

		#region Private Methods

		protected override object OnProcess(object toProcess) {
			if (IsEnable) {
				StatisticValueEntry statisticValue = StatisticValuesLocatorService[InputFrameNumberStatisticValue];
				if (statisticValue != null && statisticValue.Value is int) {
					var number = (int)statisticValue.Value;
					var seconds = (int)((double)number / FramesPerSeconds);
					var milliseconds = (int)(1000.0 / FramesPerSeconds * (number % FramesPerSeconds));
					var t = new TimeSpan(0, 0, 0, seconds, milliseconds);
					StatisticValuesProviderService.SetStatisticValue(OutputStatisticValue,
																	  string.Format("{0}:{1}:{2}:{3}", t.Hours.ToString("00"),
																					 t.Minutes.ToString("00"),
																					 t.Seconds.ToString("00"),
																					 t.Milliseconds.ToString("000")));
				}
			}
			return toProcess;
		}

		#endregion

		#region Private Fields

		// private fields located here

		#endregion
	}
}